package com.ymatou.iapi.trading.sellerquery.testcase;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.query.seller.trading.api.parameter.GetSellerOrderByIdBean;
import com.ymatou.query.seller.trading.api.parameter.Seller.Common;
import com.ymatou.query.seller.trading.api.parameter.Seller.GetSellerOrderByIdResponse;
import com.ymatou.query.seller.trading.api.parameter.info.SellerOrderInfo;
import com.ymatou.query.seller.trading.api.service.OrderCreate;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.common.entitie.C2COrderInfo;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;

/**
 * 根据单个订单号查询 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetSellerOrderById {
	private static GetSellerOrderByIdBean getsellerorderbyidBean;
	private static com.ymatou.query.seller.trading.api.service.GetSellerOrderByIdCall getsellerorderbyidCall;
//	private static com.ymatou.iapi.trading.sellerquery.service.GetSellerOrderByIdCall getsellerorderbyidCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据单个订单号查询");
	}

	@Before
	public void caseUp() {
		getsellerorderbyidBean = new GetSellerOrderByIdBean();
		getsellerorderbyidCall = new com.ymatou.query.seller.trading.api.service.GetSellerOrderByIdCall();
//		getsellerorderbyidCall = new com.ymatou.iapi.trading.sellerquery.service.GetSellerOrderByIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getsellerorderbyidCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void verify() {
		GetSellerOrderByIdResponse expectedResponse = new GetSellerOrderByIdResponse();
		ConstructExpectedResponse(expectedResponse);
		GetSellerOrderByIdResponse actualResponse = getsellerorderbyidCall
				.getSellerOrderByIdResponse();
		if (expectedResponse.getOrder() == null) {
			DataUtil.verifyEquals(null, actualResponse.getOrder(),
					"验证期望的Order=null");
		} else {
			SellerOrderInfo expect = expectedResponse.getOrder();
			int expectId = expect.getBasic().getID();
			SellerOrderInfo actual = actualResponse.getOrder();
			int actualId = actual.getBasic().getID();
			boolean flag = expectId == actualId;
			DataUtil.verifyEquals(expectId, actualId, "验证Order.Basic.ID");
			if (flag) {
				Ts_GetSellerOrders.verifySellerOrderInfo(expect, actual,
						expectId);
			}
		}
	}

	private void ConstructExpectedResponse(GetSellerOrderByIdResponse response) {
		List<Integer> orders = new ArrayList<Integer>();
		if (getsellerorderbyidBean.getOrderId() != null) {
			orders.add(getsellerorderbyidBean.getOrderId());
		}
		List<SellerOrderInfo> dingdans = Common.GetSellerOrders(
				DataUtil.Integerr.GetValueOrDefault(getsellerorderbyidBean
						.getSellerId()), orders);
		if (DataUtil.isNullOrEmpty(dingdans) == false) {
			response.setOrder(dingdans.get(0));
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderById_001() {
		Logger.start(true, "查询M订单成功");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			Logger.comment("准备前置数据");
			OrderCreate c = new OrderCreate();
			C2COrderInfo order = c.createOrderWithSellerCoupon(userId, sellerId);
			Thread.sleep(2000l);
			Logger.comment("完成准备前置数据");
			getsellerorderbyidBean.setSellerId(sellerId);
			getsellerorderbyidBean.setOrderid(order.getiOrderId());
			String param=FormatUtil.beanToHttpString(getsellerorderbyidBean, true, false);
			getsellerorderbyidCall.setData(param); 
			getsellerorderbyidCall.callService();

			Logger.verifyEquals(ECode.SUCCESS,
					getsellerorderbyidCall.getCode(), "验证Code");
			// 数据库验证
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderById_002() {
		Logger.start(true, "查询C订单成功");
		try {
			Logger.comment("准备前置数据");
			OrderCallService orderService = new OrderCallService();
			// 创建订单
			Map map = orderService.appSaveOrder();
			int orderId = (Integer) map.get("orderId");
			int sellerId = (Integer) map.get("sellerId");
			int userId = (Integer) map.get("userId");
			int tradingId = (Integer) map.get("tradingId");
			BigDecimal freight = (BigDecimal) map.get("freight");
			DecimalFormat df = new DecimalFormat("0.00");// 保留两位小数
			BigDecimal totalPrice = (BigDecimal) map.get("totalPrice");// 支付金额
			// 买家付款
			orderService.orderPayRequest(userId,
					orderService.getNeedPrePayAmountOfCash(orderId), tradingId);
			// 接单
			orderService.acceptOrder(orderId, sellerId);
			// 卖家发起补款
			int price = 1 + new Random().nextInt(50);
			BigDecimal payAmount = new BigDecimal(df.format(price));
			String postPayId = orderService.applyForPostPay(orderId, sellerId,
					payAmount);
			// 买家补款
			orderService.orderPostPayRequest(userId, payAmount, orderId);
			// 商家发货
			orderService.dispatched(orderId, sellerId);
			// 确认收货
			orderService.confirmReceive(userId, orderId);
			Logger.comment("准备前置数据结束");
			Thread.sleep(2000l);
			// int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			getsellerorderbyidBean.setSellerId(sellerId);
			getsellerorderbyidBean.setOrderid(orderId);
			
			String param=FormatUtil.beanToHttpString(getsellerorderbyidBean, true, false);
			getsellerorderbyidCall.setData(param); 
			getsellerorderbyidCall.callService();

			Logger.verifyEquals(ECode.SUCCESS,
					getsellerorderbyidCall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetSellerOrderById_003() {
		Logger.start(false, "卖家id为null");
		try {
			getsellerorderbyidBean=new GetSellerOrderByIdBean();
			// getsellerorderbyidBean.setSellerId(20001501);
			getsellerorderbyidBean.setOrderid(105416725);
			getsellerorderbyidCall.setData(getsellerorderbyidBean);
			getsellerorderbyidCall.callService();

			Logger.verifyEquals(ECode.ERROR400,
					getsellerorderbyidCall.getCode(), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetSellerOrderById_004() {
		Logger.start(false, "卖家id为0");
		try {
			getsellerorderbyidBean.setSellerId(0);
			getsellerorderbyidBean.setOrderid(105416725);
			
			String param=FormatUtil.beanToHttpString(getsellerorderbyidBean, true, false);
			getsellerorderbyidCall.setData(param); 
			getsellerorderbyidCall.callService();

			Logger.verifyEquals(ECode.ERROR400,
					getsellerorderbyidCall.getCode(), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetSellerOrderById_005() {
		Logger.start(false, "订单id为null");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			getsellerorderbyidBean.setSellerId(sellerId);
			getsellerorderbyidBean.setOrderid(0);

			String param=FormatUtil.beanToHttpString(getsellerorderbyidBean, true, false);
			getsellerorderbyidCall.setData(param); 
			getsellerorderbyidCall.callService();

			Logger.verifyEquals(ECode.ERROR400,
					getsellerorderbyidCall.getCode(), "验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetSellerOrderById_006() {
		Logger.start(false, "订单id为0");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			getsellerorderbyidBean.setSellerId(sellerId);
			getsellerorderbyidBean.setOrderid(0);
			
			String param=FormatUtil.beanToHttpString(getsellerorderbyidBean, true, false);
			getsellerorderbyidCall.setData(param); 
			getsellerorderbyidCall.callService();

			Logger.verifyEquals(ECode.ERROR400,
					getsellerorderbyidCall.getCode(), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderById_007() {
		Logger.start(true, "查询M订单成功, 商品有退货信息");
		try {
			Logger.comment("准备前置数据");
			OrderCallService orderService = new OrderCallService();
			PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
			int userId =placeOrderResp.getUserId();         
			int tradingId = placeOrderResp.getTradingId();
			BigDecimal freight = placeOrderResp.getFreight(); // add by
																	// sonegyefeng
			BigDecimal totalPrice =placeOrderResp.getPayAmount();// 支付金额
			// 买家付款
			orderService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderService.acceptOrder(orderId, sellerId);
			// 商家发货
			orderService.dispatched(orderId, sellerId);
			// 确认收货
			orderService.confirmReceive(userId, orderId);
			// 退货
			orderService.salesRefundSuccessRequestInJava(orderId);

			Thread.sleep(2000l);
			// orderService.salesRefundStatusSync(orderId, 3);
			Logger.comment("准备前置数据结束");
			// int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			getsellerorderbyidBean.setSellerId(sellerId);
			getsellerorderbyidBean.setOrderid(orderId);

			String param=FormatUtil.beanToHttpString(getsellerorderbyidBean, true, false);
			getsellerorderbyidCall.setData(param); 
			getsellerorderbyidCall.callService();

			Logger.verifyEquals(ECode.SUCCESS,
					getsellerorderbyidCall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}